﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]随机数辅助类
*********************************************************************************/
using System;
using System.Globalization;
using System.Threading;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    /// [辅助类]随机数辅助类
    /// </summary>
    public sealed class RandUtil
    {
        #region 构造实例

        /// <summary>
        /// 随机数辅助类
        /// </summary>
        private RandUtil()
        {
        }

        /// <summary>
        /// 构建类的实例对象
        /// </summary>
        public static RandUtil Instance
        {
            get { return new RandUtil(); }
        }
        #endregion 构造实例

        #region 公共方法

        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <returns>返回生成的随机数</returns>
        public string Number(int length)
        {
            return Number(length, false);
        }

        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns>返回生成的随机数</returns>
        public string Number(int length, bool sleep)
        {
            if (sleep)
                Thread.Sleep(20);
            string result = "";
            var random = new Random();
            for (int i = 0; i < length; i++)
            {
                result += random.Next(10).ToString(CultureInfo.InvariantCulture);
            }
            return result;
        }

        /// <summary>
        /// 生成随机字母与数字
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <returns>返回生成的随机数</returns>
        public string Str(int length)
        {
            return Str(length, false);
        }

        /// <summary>
        /// 生成随机字母与数字
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns>返回生成的随机数</returns>
        public string Str(int length, bool sleep)
        {
            if (sleep)
                Thread.Sleep(3);
            var pattern = new[]
                              {
                                  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                                  'H',
                                  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
                                  'Z'
                              };
            string result = "";
            int n = pattern.Length;
            var random = new Random(~unchecked((int) DateTime.Now.Ticks));
            for (int i = 0; i < length; i++)
            {
                int rnd = random.Next(0, n);
                result += pattern[rnd];
            }
            return result;
        }

        /// <summary>
        /// 生成随机纯字母随机数
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <returns>返回生成的随机数</returns>
        public string Char(int length)
        {
            return Char(length, false);
        }

        /// <summary>
        /// 生成随机纯字母随机数
        /// </summary>
        /// <param name="length">生成长度</param>
        /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns>返回生成的随机数</returns>
        public string Char(int length, bool sleep)
        {
            if (sleep)
                Thread.Sleep(3);
            var pattern = new[]
                              {
                                  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                                  'R',
                                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
                              };
            string result = "";
            int n = pattern.Length;
            var random = new Random(~unchecked((int) DateTime.Now.Ticks));
            for (int i = 0; i < length; i++)
            {
                int rnd = random.Next(0, n);
                result += pattern[rnd];
            }
            return result;
        }

        #endregion 公共方法
    }
}